﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HexCells : MonoBehaviour
{
    #region 数据列表
    HexStatus hexStatus;

    [SerializeField]
    public bool isUnlocked;

    public Transform ImageGroup;
    
    public Transform Grass;

    public Transform Mountain;

    public Transform Forest;

    private StructInfo m_structInfo;

    private Button[] buttonGroup;//按钮组

    private Button button1;

    private Button button2;

    private Button button3;

    #endregion
    private void Start()
    {
        isUnlocked = false;
        InitImages();
        InitUIConponents();
        MapInit.hexCells.Add(gameObject);
        ChangeStatusRandomly();
    }

    //public void GenerateHexCells()
    //{
    //   switch(hexStatus)
    //    {
    //        case HexStatus.Grass:
    //            //Instantiate(game, GetPosition(position),Quaternion.identity);
    //            //TODO:在相应位置生成的逻辑
    //            break;
    //        case HexStatus.Mountain://即未解锁状态                
    //            //Instantiate(game, GetPosition(position), Quaternion.identity);
    //            break;
    //        case HexStatus.Forest:

    //            break;
    //    }
    //}

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            Debug.Log("2324");
            SwitchImageByStatus();
        }
    }

    public Vector2 GetHexPosition()
    {
        return transform.position;
    }

    public void SwitchImageByStatus()
    {
        if(!ImageGroup.gameObject.activeSelf)
        {
            ImageGroup.gameObject.SetActive(true);
            switch(hexStatus)
            {
                case HexStatus.Grass:
                    Grass.gameObject.SetActive(true);
                    break;
                case HexStatus.Mountain:
                    Mountain.gameObject.SetActive(true);
                    break;
                case HexStatus.Forest:
                    Forest.gameObject.SetActive(true);
                    break;
            }
        }
        else
        {
            ImageGroup.gameObject.SetActive(false);
            switch(hexStatus)
            {
                case HexStatus.Grass:
                    Grass.gameObject.SetActive(false);
                    break;
                case HexStatus.Mountain:
                    Mountain.gameObject.SetActive(false);
                    break;
                case HexStatus.Forest:
                    Forest.gameObject.SetActive(false);
                    break;
            }
        }
    }

    public void ChangeStatusRandomly()
    {
        hexStatus = (HexStatus)Random.Range(1, 3);  //随机生成切换类型
    }

    public void InitImages()
    {
        ImageGroup = transform.Find("ImageGroup");
        if(ImageGroup != null) { Debug.Log("1111111"); }
        Grass = ImageGroup.Find("Grass");
        Mountain = ImageGroup.Find("Mountain");
        Forest = ImageGroup.Find("Forest");
        ImageGroup.gameObject.SetActive(false);
        Grass.gameObject.SetActive(false);
        Mountain.gameObject.SetActive(false);
        Forest.gameObject.SetActive(false);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            SwitchImageByStatus();
        }
    }

    private void OnMouseDown()
    {
        gameObject.AddComponent<StructInfo>();
        m_structInfo = GetComponent<StructInfo>();
        //UI Menu设置
        PrintButtons();
        //TODO
    }


    #region UI相关
    private void InitUIConponents()
    {
        buttonGroup = GetComponents<Button>();
        foreach (Button button in buttonGroup)
        {
            if (button.name == "button1")
            {
                button1 = button;
                button1.gameObject.SetActive(false);
            }
            if (button.name == "button2")
            {
                button2 = button;
                button2.gameObject.SetActive(false);
            }
            if (button.name == "button3")
            {
                button3 = button;
                button3.gameObject.SetActive(false);
            }
        }
    }//UI初始化

    private void PrintButtons()
    {
        foreach(Button button in buttonGroup)
        {
            button.gameObject.SetActive(true);
            Debug.Log("Buttons Printed!");
        }
    }

    private void CloseButtons()
    {
        foreach (Button button in buttonGroup)
        {
            button.gameObject.SetActive(false);
            Debug.Log("Buttons Sealed!");
        }
    }

    // 三个button的点击事件 分别代表了三个相应的建筑
    public void OnButton1Clicked()
    {
        m_structInfo.structInfo = StructInformation.Land;
        CloseButtons();
        m_structInfo.OnStatusChanged();
    }

    public void OnButton2Clicked()
    {

        CloseButtons();
    }

    public void OnButton3Clicked()
    {

        CloseButtons();
    }
    #endregion
}